package com.wesh.gateway.filters;

import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;

@Component
@Order(-1)
public class CheckAuthFilter implements GlobalFilter {

    private final Logger logger = LoggerFactory.getLogger(CheckAuthFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        logger.info("校验全局用户权限");
        List<String> token = exchange.getRequest().getHeaders().get("token");
        if(token.isEmpty()){
            return null;
        }
        return chain.filter(exchange);
    }
}
